package com.example.mobile2032021606168xmy0612;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private EditText edtTxtMainName;
    private EditText edtTxtMainPhonenumber;
    private Button btnMainInsert;
    private Button btnMainDelete;
    private Button btnMainUpdate;
    private Button btnMainCheckall;
    private Button btnMainCheckone;
    private TextView tvMainContent;

    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        openHelper = new MySQLiteOpenHelper(this,"addresslist.db",null,1);
        btnMainInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtTxtMainName.getText().toString().trim();
                int phonenumber = Integer.valueOf(edtTxtMainPhonenumber.getText().toString().trim());

                ContentValues values = new ContentValues();
                values.put("name",name);
                values.put("phonenumber",phonenumber);

                database=openHelper.getWritableDatabase();
                long result = database.insert("users",null,values);
                database.close();
                Log.i("TAG","onClick:"+result);

            }
        });
        btnMainCheckall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                database=openHelper.getReadableDatabase();
                Cursor cursor = database.query("users",
                        null,null,null,
                        null,null,null);
                if (cursor.moveToFirst()){
                    tvMainContent.setText(null);
                    do {
                        String name = cursor.getString(1);
                        int phonenumber=cursor.getInt(2);
                        tvMainContent.append(name+" "+phonenumber+"  "+"\n");
                    }while (cursor.moveToNext());
                }

                cursor.close();
                database.close();
            }
        });
        btnMainCheckone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                database=openHelper.getReadableDatabase();
                Cursor cursor = database.query("users",
                        null,null,null,
                        null,null,null);
                if (cursor.moveToFirst()){
                    tvMainContent.setText(null);
                    do {
                        String name = cursor.getString(1);
                        int phonenumber=cursor.getInt(2);
                        tvMainContent.append(name+" "+phonenumber+"  ");
                    }while (cursor.moveToNext());
                }

                cursor.close();
                database.close();

            }
        });
        btnMainUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=edtTxtMainName.getText().toString().trim();
                float phonenumber= Float.valueOf(edtTxtMainPhonenumber.getText().toString().trim());
                ContentValues values =new ContentValues();
                values.put("phonenumber",phonenumber);

                database = openHelper.getWritableDatabase();
                database.update("users",values,"name=?",new String[]{name});
                database.close();
            }
        });
        btnMainDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=edtTxtMainName.getText().toString().trim();
                database=openHelper.getWritableDatabase();
                database.delete("users","name=?",new String[]{name});
            }
        });

    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        edtTxtMainName = (EditText) findViewById(R.id.edtTxt_main_name);
        edtTxtMainPhonenumber = (EditText) findViewById(R.id.edtTxt_main_phonenumber);
        btnMainInsert = (Button) findViewById(R.id.btn_main_insert);
        btnMainDelete = (Button) findViewById(R.id.btn_main_delete);
        btnMainUpdate = (Button) findViewById(R.id.btn_main_update);
        btnMainCheckall = (Button) findViewById(R.id.btn_main_checkall);
        btnMainCheckone = (Button) findViewById(R.id.btn_main_checkone);
        tvMainContent = (TextView) findViewById(R.id.tv_main_content);
    }
}